<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <h1 id="logo">
          <a href="#" title="江南大学">
            <img src="~/assets/img/11.jpg" width="100%" alt="江南大学">
          </a>
        </h1>
        <div class="h-r-nsl">
          <ul class="nav">
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>名师</a>
            </router-link>
            <router-link to="/article" tag="li" active-class="current">
              <a>文章</a>
            </router-link>
            <router-link to="/qa" tag="li" active-class="current">
              <a>问答</a>
            </router-link>
          </ul>
          <!-- / nav -->
          <ul class="h-r-login">
            <li v-if="!userInfo.id" id="no-login">
              <a href="/login" title="登录">
                <em class="icon18 login-icon">&nbsp;</em>
                <span class="vam ml5">登录</span>
              </a>
              |
              <a href="/register" title="注册">
                <span class="vam ml5">注册</span>
              </a>
            </li>
            <!--如果已经登录则显示用户的头像和昵称-->
            <li v-if="userInfo.id" class="mr10" id="is-login-one">
              <a href="#" title="消息" id="headerMsgCountId">
                <em class="icon18 news-icon">&nbsp;</em>
              </a>
              <q class="red-point" style="display: none">&nbsp;</q>
            </li>
            <li v-if="userInfo.id" class="h-r-user" id="is-login-two">
              <a href="/ucenter" title>
                <img
                  :src="userInfo.avatar"
                  height="30"
                  class="vam picImg"
                  alt
                >
                <span class="vam disIb" id="userName">{{userInfo.nickname}}</span>
              </a>
              <a href="#" title="退出" @click="loginOut" class="ml5">退出</a>
            </li>
            <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search">
            <form action="#" method="post">
              <label class="h-r-s-box">
                <input type="text" placeholder="输入课程信息" name="queryCourse.courseName" value>
                <button type="submit" class="s-btn">
                  <em class="icon18">&nbsp;</em>
                </button>
              </label>
            </form>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>

    <!-- /公共头引入 -->
    <nuxt/>

    <!-- 公共底引入 -->
    <footer id="footer">
      <section class="container">
        <div class>
          <h4 class="hLh30">
            <span class="fsize18 f-fM c-999">友情链接</span>
          </h4>
          <ul class="of flink-list">
            <li>
              <a href="https://www.jiangnan.edu.cn/" title="江南大学" target="_blank">江南大学</a>
            </li>
            <li>
              <a href="http://39.102.67.107:8080/myshop/" title="电商平台" target="_blank">我的电商后台</a>
            </li>
          </ul>
          <div class="clear"></div>
        </div>
        <div class="b-foot">
          <section class="fl col-7">
            <section class="mr20">
              <section class="b-f-link">
                <a href="#" title="关于我们" target="_blank">关于我们</a>|
                <a href="#" title="联系我们" target="_blank">联系我们</a>|
                <a href="#" title="帮助中心" target="_blank">帮助中心</a>|
                <a href="#" title="资源下载" target="_blank">资源下载</a>|
                <span>服务热线：18151534755</span>
                <span>Email：2753914576.com</span>
              </section>
              <section class="b-f-link mt10">
                <span>©2020 版权所有 -- LHM --</span>
              </section>
            </section>
          </section>
          <aside class="fl col-3 tac mt15">
            <section class="gf-tx">
              <span>
                <img src="~/assets/img/wx-icon.png" alt>
              </span>
            </section>
            <section class="gf-tx">
              <span>
                <img src="~/assets/img/wb-icon.png" alt>
              </span>
            </section>
          </aside>
          <div class="clear"></div>
        </div>
      </section>
    </footer>
    <!-- /公共底引入 -->
  </div>
</template>
<script>
  import "~/assets/css/reset.css";
  import "~/assets/css/theme.css";
  import "~/assets/css/global.css";
  import "~/assets/css/web.css";
  import '~/assets/css/base.css'
  import '~/assets/css/activity_tab.css'
  import '~/assets/css/bottom_rec.css'
  import '~/assets/css/nice_select.css'
  import '~/assets/css/order.css'
  import '~/assets/css/swiper-3.3.1.min.css'
  import "~/assets/css/pages-weixinpay.css"

  import cookie from 'js-cookie'
  import loginApi from '@/api/login'
  export default {
    data() {
      return {
        token: '',
        //用户信息
        userInfo: {},
      }
    },
    created() {
      this.token = this.$route.query.token;
      if (this.token) {
        this.wxGetUserInfo();
      }else {
        this.getUserInfoFromCookie();
      }
    },
    methods: {
      //从cookie中获取用户信息
      getUserInfoFromCookie() {
        //由于存放在cookie中的userInfo为json字符串
        //需要将字符串转为JSON对象
        let result = cookie.get('jn_user');
        if (result) {
          this.userInfo = JSON.parse(result);
        }
      },

      //登出事件
      loginOut() {
        this.userInfo = {};
        //从cookie中删除对应的user信息，没有直接的删除方法，需要再次设置
        cookie.set('jn_cookie', '', {domain: 'localhost'});
        cookie.set('jn_user', '', {domain: 'localhost'});
        this.$router.push({path: '/'});
      },

      //处理微信登录事件用来获取用户信息
      async wxGetUserInfo(){
        cookie.set('jn_cookie', this.token, {domain: 'localhost'});
        const {data} = await loginApi.getUserInfo();
        if(data.code !== 20000) return this.$message.error(data.message);
        this.userInfo = data.data.data;
        cookie.set('jn_user', this.userInfo, {domain: 'localhost'});
      }
    }
  };
</script>
